package com.cmc.cloud.cmclink.doc.api.document;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmc.cloud.cmclink.doc.api.document.emums.CntrOwnerEnum;
import com.cmc.cloud.cmclink.doc.api.document.emums.SiStatusEnum;
import com.cmc.cloud.cmclink.doc.api.document.emums.SiValidEnum;
import com.cmc.cloud.cmclink.doc.api.document.emums.SoSiEnum;
import com.cmc.cloud.cmclink.doc.api.document.rep.*;
import com.cmc.cloud.cmclink.doc.api.document.req.AttachmentUpdateReqDto;
import com.cmc.cloud.cmclink.doc.api.document.req.BlnoInfoReqDto;
import com.cmc.cloud.cmclink.doc.api.document.req.BlnoInfoReqMktDto;
import com.cmc.cloud.cmclink.doc.api.document.req.si.*;
import com.cmc.cloud.cmclink.doc.api.document.req.so.SoForecastRespVO;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BlnoErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BlnoInfoErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.*;
import com.cmc.cloud.cmclink.doc.entity.*;
import com.cmc.cloud.cmclink.doc.mapper.*;
import com.cmc.cloud.cmclink.doc.service.si.BlnoService;
import com.cmc.cloud.cmclink.doc.vo.blnovo.BlnoForecastReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnovo.BlnoMultipleSoCreateVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 提单信息查询接口实现类
 *
 * @author lzp
 * @date 2025/8/12
 */
@RestController
@Valid
public class BlnoInfoQueryApiImpl implements BlnoInfoQueryApi {

    @Resource
    private BlnoMapper blnoMapper;

    @Resource
    private BlnoVslvoyMapper blnoVslvoyMapper;

    @Resource
    private BlnoCargoMapper blnoCargoMapper;

    @Resource
    private BlnoCntrMapper blnoCntrMapper;

    @Resource
    private BlnoFreightMapper blnoFreightMapper;

    @Resource
    private BlnoFreightDetailMapper blnoFreightDetailMapper;

    @Resource
    private BlnoLocalChargeMapper blnoLocalChargeMapper;

    @Resource
    private BlnoLocalChargeDetailMapper blnoLocalChargeDetailMapper;

    @Resource
    private BlnoPaymentMapper blnoPaymentMapper;

    @Resource
    private BlnoScnMapper blnoScnMapper;
    @Resource
    private AttachmentMapper attachmentMapper;
    @Resource
    private BlnoService blnoService;
    @Resource
    private BlnoMultipleSoMapper blnoMultipleSoMapper;

    private static final int BLNOS_SIZE = 10;

    private static final List<String> SI_STATUS = Arrays.asList(SiStatusEnum.BookingPendingSpace.getValue(), SiStatusEnum.BookingRejected.getValue()
            , SiStatusEnum.AwaitingMaterialUploaded.getValue(), SiStatusEnum.BookingConfirmed.getValue());

    @Override
    public CommonResult<List<BlnoInfoRespDto>> querySiInfo(List<String> blNos) {
        if (CollectionUtils.isEmpty(blNos)) {
            return CommonResult.success(Collections.emptyList());
        }
        if (blNos.size() > BLNOS_SIZE) {
            throw ServiceExceptionUtil.exception(BlnoInfoErrorCodeConstants.BLNO_INFO_ERROR_OUT_COUNT);
        }
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getBlNo, blNos)
                .eq(BlnoDO::getSpaceOwner, CntrOwnerEnum.SNL.getValue())
                .eq(BlnoDO::getBlLockFlag, true)
                .eq(BlnoDO::getPodBlFlag, true)
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .ne(BlnoDO::getSiStatus, SiStatusEnum.BookingDraft.getValue());
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        Map<Long, List<BlnoDO>> blnoDoMap = blnoDOList.stream().collect(Collectors.groupingBy(BlnoDO::getId));
        return CommonResult.success(querySiInfoByMap(blnoDoMap));
    }

    @Override
    public CommonResult<List<BlnoInfoRespDto>> querySiEdiInfo(@Valid BlnoEdiReqDto blnoEdiReqDto) {
        checkEdiData(blnoEdiReqDto);
        List<String> blNos = blnoEdiReqDto.getBlNoList();
        if (CollectionUtils.isEmpty(blNos)) {
            return CommonResult.success(Collections.emptyList());
        }
        if (blNos.size() > BLNOS_SIZE) {
            throw ServiceExceptionUtil.exception(BlnoInfoErrorCodeConstants.BLNO_INFO_ERROR_OUT_COUNT);
        }
        List<BlnoDO> blnoDos = blnoMapper.selectList(new LambdaQueryWrapperX<BlnoDO>().in(BlnoDO::getBlNo, blNos).eq(BlnoDO::getValid, SiValidEnum.VALID.getValue()));
        List<Long> blnoIds = blnoDos.stream().map(BlnoDO::getId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(blnoIds)){
            return CommonResult.success(Collections.emptyList());
        }
        List<BlnoVslvoyDO> blnoVslvoyDos = blnoVslvoyMapper.selectList(new LambdaQueryWrapperX<BlnoVslvoyDO>()
                .eqIfPresent(BlnoVslvoyDO::getPolCode, blnoEdiReqDto.getPolCode())
                .eqIfPresent(BlnoVslvoyDO::getPodCode, blnoEdiReqDto.getPodCode())
                .eq(BlnoVslvoyDO::getVesselCode, blnoEdiReqDto.getVesselCode())
                .eqIfPresent(BlnoVslvoyDO::getVoyageEx, blnoEdiReqDto.getVoyageEx())
                .eqIfPresent(BlnoVslvoyDO::getVoyageIm, blnoEdiReqDto.getVoyageIm())
                .inIfPresent(BlnoVslvoyDO::getBlnoId, blnoIds));
        if (CollectionUtils.isEmpty(blnoVslvoyDos)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<Long> filterBlnoIds = blnoVslvoyDos.stream().map(BlnoVslvoyDO::getBlnoId).collect(Collectors.toList());
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getId, filterBlnoIds)
                .eq(BlnoDO::getPodBlFlag, true)
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .in(BlnoDO::getSiStatus, SI_STATUS);
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        Map<Long, List<BlnoDO>> blnoDoMap = blnoDOList.stream().collect(Collectors.groupingBy(BlnoDO::getId));
        return CommonResult.success(querySiInfoByMap(blnoDoMap));
    }

    @Override
    public CommonResult<List<BlnoMainInfoEdiRespDto>> querySiEdiMainInfo(@Valid BlnoEdiReqBaseDto blnoEdiReqBaseDto) {
        checkEdiData(blnoEdiReqBaseDto);
        List<BlnoVslvoyDO> blnoVslvoyDos = blnoVslvoyMapper.selectList(new LambdaQueryWrapperX<BlnoVslvoyDO>()
                .eqIfPresent(BlnoVslvoyDO::getPolCode, blnoEdiReqBaseDto.getPolCode())
                .eqIfPresent(BlnoVslvoyDO::getPodCode, blnoEdiReqBaseDto.getPodCode())
                .eq(BlnoVslvoyDO::getVesselCode, blnoEdiReqBaseDto.getVesselCode())
                .eqIfPresent(BlnoVslvoyDO::getVoyageEx, blnoEdiReqBaseDto.getVoyageEx())
                .eqIfPresent(BlnoVslvoyDO::getVoyageIm, blnoEdiReqBaseDto.getVoyageIm()));
        if (CollectionUtils.isEmpty(blnoVslvoyDos)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<Long> filterBlnoIds = blnoVslvoyDos.stream().map(BlnoVslvoyDO::getBlnoId).collect(Collectors.toList());
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getId, filterBlnoIds)
                .eq(BlnoDO::getPodBlFlag, true)
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .inIfPresent(BlnoDO::getSiStatus, SI_STATUS);
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(BeanUtil.copyToList(blnoDOList, BlnoMainInfoEdiRespDto.class));
    }

    private void checkEdiData(BlnoEdiReqBaseDto blnoEdiReqBaseDto) {
        if (StringUtils.isEmpty(blnoEdiReqBaseDto.getPodCode()) && StringUtils.isEmpty(blnoEdiReqBaseDto.getPolCode())) {
            throw ServiceExceptionUtil.exception(BlnoErrorCodeConstants.SI_POL_OR_POD_NOT);
        }
        if (StringUtils.isEmpty(blnoEdiReqBaseDto.getVoyageEx()) && StringUtils.isEmpty(blnoEdiReqBaseDto.getVoyageIm())) {
            throw ServiceExceptionUtil.exception(BlnoErrorCodeConstants.SI_VOYAGE_IM_EX_NOT);
        }
    }

    @Override
    public CommonResult<List<BlnoInfoRespDto>> querySiInfoByIds(List<Long> blIds) {
        if (CollectionUtils.isEmpty(blIds)) {
            return CommonResult.success(Collections.emptyList());
        }
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getId, blIds).eq(BlnoDO::getValid, SiValidEnum.VALID.getValue());
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        Map<Long, List<BlnoDO>> blnoDoMap = blnoDOList.stream().collect(Collectors.groupingBy(BlnoDO::getId));
        return CommonResult.success(querySiInfoByMap(blnoDoMap));
    }

    private List<BlnoInfoRespDto> querySiInfoByMap(Map<Long, List<BlnoDO>> blnoDoMap) {
        List<Long> blIds = new ArrayList<>(blnoDoMap.keySet());

        //查询船期
        LambdaQueryWrapperX<BlnoVslvoyDO> blnoVslvoyWrapperX = new LambdaQueryWrapperX<>();
        blnoVslvoyWrapperX.inIfPresent(BlnoVslvoyDO::getBlnoId, blIds);
        Map<Long, List<BlnoVslvoyDO>> blnoVslvoyDoMap = blnoVslvoyMapper.selectList(blnoVslvoyWrapperX).stream().collect(Collectors.groupingBy(BlnoVslvoyDO::getBlnoId));
        //查询货物信息
        LambdaQueryWrapperX<BlnoCargoDO> blnoCargoWrapperX = new LambdaQueryWrapperX<>();
        blnoCargoWrapperX.inIfPresent(BlnoCargoDO::getBlnoId, blIds);
        Map<Long, List<BlnoCargoDO>> blnoCargoDoMap = blnoCargoMapper.selectList(blnoCargoWrapperX).stream().collect(Collectors.groupingBy(BlnoCargoDO::getBlnoId));
        //查询箱信息
        LambdaQueryWrapperX<BlnoCntrDO> blnoCntrWrapperX = new LambdaQueryWrapperX<>();
        blnoCntrWrapperX.inIfPresent(BlnoCntrDO::getBlnoId, blIds);
        Map<Long, List<BlnoCntrDO>> blnoCntrDoMap = blnoCntrMapper.selectList(blnoCntrWrapperX).stream().collect(Collectors.groupingBy(BlnoCntrDO::getBlnoId));
        //海运费信息
        LambdaQueryWrapperX<BlnoFreightDO> blnoFreightWrapperX = new LambdaQueryWrapperX<>();
        blnoFreightWrapperX.inIfPresent(BlnoFreightDO::getBlnoId, blIds);
        Map<Long, List<BlnoFreightDO>> blnoFreightDoMap = blnoFreightMapper.selectList(blnoFreightWrapperX).stream().collect(Collectors.groupingBy(BlnoFreightDO::getBlnoId));
        //海运费详情信息
        LambdaQueryWrapperX<BlnoFreightDetailDO> blnoFreightDetailWrapperX = new LambdaQueryWrapperX<>();
        blnoFreightDetailWrapperX.inIfPresent(BlnoFreightDetailDO::getBlnoId, blIds);
        Map<Long, List<BlnoFreightDetailDO>> blnoFreightDetailDoMap = blnoFreightDetailMapper.selectList(blnoFreightDetailWrapperX).stream().collect(Collectors.groupingBy(BlnoFreightDetailDO::getBlnoId));
        //local charge费用
        LambdaQueryWrapperX<BlnoLocalChargeDO> blnoLocalChargeWrapperX = new LambdaQueryWrapperX<>();
        blnoLocalChargeWrapperX.inIfPresent(BlnoLocalChargeDO::getBlnoId, blIds);
        Map<Long, List<BlnoLocalChargeDO>> blnoLocalChargeDoMap = blnoLocalChargeMapper.selectList(blnoLocalChargeWrapperX).stream().collect(Collectors.groupingBy(BlnoLocalChargeDO::getBlnoId));
        //local charge detail费用
        LambdaQueryWrapperX<BlnoLocalChargeDetailDO> blnoLocalChargeDetailWrapperX = new LambdaQueryWrapperX<>();
        blnoLocalChargeDetailWrapperX.inIfPresent(BlnoLocalChargeDetailDO::getBlnoId, blIds);
        Map<Long, List<BlnoLocalChargeDetailDO>> blnoLocalChargeDetailDoMap = blnoLocalChargeDetailMapper.selectList(blnoLocalChargeDetailWrapperX).stream().collect(Collectors.groupingBy(BlnoLocalChargeDetailDO::getBlnoId));
        //提单收发通信息
        LambdaQueryWrapperX<BlnoScnDO> blnoScnWrapperX = new LambdaQueryWrapperX<>();
        blnoScnWrapperX.inIfPresent(BlnoScnDO::getBlnoId, blIds);
        Map<Long, List<BlnoScnDO>> blnoScnDoMap = blnoScnMapper.selectList(blnoScnWrapperX).stream().collect(Collectors.groupingBy(BlnoScnDO::getBlnoId));
        //支付方式信息
        LambdaQueryWrapperX<BlnoPaymentDO> blnoPaymentWrapperX = new LambdaQueryWrapperX<>();
        blnoPaymentWrapperX.inIfPresent(BlnoPaymentDO::getBlnoId, blIds);
        Map<Long, List<BlnoPaymentDO>> blnoPaymentDoMap = blnoPaymentMapper.selectList(blnoPaymentWrapperX).stream().collect(Collectors.groupingBy(BlnoPaymentDO::getBlnoId));
        //构建返回参数
        List<BlnoInfoRespDto> blnoInfoRespDtoList = new ArrayList<>(16);
        for (Long key : blnoDoMap.keySet()) {
            BlnoInfoRespDto blnoInfoRespDto = new BlnoInfoRespDto();
            List<BlnoDO> blnoDoS = blnoDoMap.get(key);
            if (CollectionUtils.isNotEmpty(blnoDoS)) {
                //基础信息
                blnoInfoRespDto.setBlnoBaseInfoRespDto(BlnoConvert.INSTANCE.convertBlnoBaseInfoRespDto(blnoDoS.get(0)));
            }
            //船名航次
            blnoInfoRespDto.setBlnoVslvoyDtoList(BlnoVslvoyConvert.INSTANCE.convertBlnoVslvoyRespDto(blnoVslvoyDoMap.get(key)));
            //货物信息
            blnoInfoRespDto.setBlnoCargoDtoList(BlnoCargoConvert.INSTANCE.convertBlnoCargoRespDto(blnoCargoDoMap.get(key)));
            //箱信息
            blnoInfoRespDto.setBlnoCntrDtoList(BeanUtil.copyToList(blnoCntrDoMap.get(key), BlnoCntrRespDto.class));
            //海运费信息
            blnoInfoRespDto.setBlnoFreightBaseDtoList(BlnoFreightConvert.INSTANCE.convertBlnoFreightBaseDto(blnoFreightDoMap.get(key)));
            //海运费详细信息
            blnoInfoRespDto.setBlnoFreightDetailDtoList(BlnoFreightDetailConvert.INSTANCE.convertBlnoFreightDetailDto(blnoFreightDetailDoMap.get(key)));
            //local charge 基本信息
            blnoInfoRespDto.setBlnoLocalChargeBaseDtoList(BlnoLocalChargeConvert.INSTANCE.convertBlnoLocalChargeBaseDto(blnoLocalChargeDoMap.get(key)));
            ////local charge 详细信息
            blnoInfoRespDto.setBlnoLocalChargeDetailBaseDtoList(BlnoLocalChargeDetailConvert.INSTANCE.convertBlnoLocalChargeDetailBaseDto(blnoLocalChargeDetailDoMap.get(key)));
            //收发通信息
            blnoInfoRespDto.setBlnoScnRespDtoList(BlnoScnConvert.INSTANCE.convertBlnoScnRespDto(blnoScnDoMap.get(key)));
            //支付方式信息
            blnoInfoRespDto.setPaymentList(BeanUtil.copyToList(blnoPaymentDoMap.get(key), BlnoPaymentBaseRespDto.class));
            blnoInfoRespDtoList.add(blnoInfoRespDto);
        }
        return blnoInfoRespDtoList;
    }

    /**
     * 查询条件是list对象为了避免多次查询，将list对象的查询条件转为list<string>通过in的方式查询然后在过滤掉
     * 条件
     *
     * @param blnoInfoReqDtoList
     * @return
     */
    @Override
    public CommonResult<List<BlnoRespDto>> queryBlNo(List<BlnoInfoReqDto> blnoInfoReqDtoList) {
        if (null == blnoInfoReqDtoList) {
            return CommonResult.success(Collections.emptyList());
        }
        List<String> polCodes = blnoInfoReqDtoList.stream().map(BlnoInfoReqDto::getPolCode).collect(Collectors.toList());
        List<String> bkgVesselCodes = blnoInfoReqDtoList.stream().map(BlnoInfoReqDto::getBkgVesselCode).collect(Collectors.toList());
        List<String> bkgVoyages = blnoInfoReqDtoList.stream().map(BlnoInfoReqDto::getBkgVoyage).collect(Collectors.toList());
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getBkgVesselCode, bkgVesselCodes)
                .inIfPresent(BlnoDO::getBkgVoyage, bkgVoyages)
                .inIfPresent(BlnoDO::getPolCode, polCodes)
                .eq(BlnoDO::getSpaceOwner, CntrOwnerEnum.SNL.getValue())
                .eq(BlnoDO::getBlLockFlag, true)
                .eq(BlnoDO::getPodBlFlag, true)
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .ne(BlnoDO::getSiStatus, SiStatusEnum.BookingDraft.getValue());
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(BlnoConvert.INSTANCE.convertBlnoRespDtos(blnoDOList));
    }

    @Override
    public CommonResult<List<BlnoRespMktDto>> queryBlnoUserByMkt(List<BlnoInfoReqMktDto> blnoInfoReqMktDtoList) {
        if (null == blnoInfoReqMktDtoList) {
            return CommonResult.success(Collections.emptyList());
        }
        List<String> porCodes = blnoInfoReqMktDtoList.stream().filter(item -> StringUtils.isNotEmpty(item.getPorCode())).map(BlnoInfoReqMktDto::getPorCode).collect(Collectors.toList());
        List<String> bkgVesselCodes = blnoInfoReqMktDtoList.stream().map(BlnoInfoReqMktDto::getBkgVesselCode).collect(Collectors.toList());
        List<String> bkgVoyages = blnoInfoReqMktDtoList.stream().map(BlnoInfoReqMktDto::getBkgVoyage).collect(Collectors.toList());
        List<String> bkpCodes = blnoInfoReqMktDtoList.stream().map(BlnoInfoReqMktDto::getBkpCode).collect(Collectors.toList());

        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getBkgVesselCode, bkgVesselCodes)
                .inIfPresent(BlnoDO::getBkgVoyage, bkgVoyages)
                .inIfPresent(BlnoDO::getPorCode, porCodes)
                .inIfPresent(BlnoDO::getBkpCode, bkpCodes)
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .eq(BlnoDO::getSpaceOwner, CntrOwnerEnum.SNL.getValue())
                .eq(BlnoDO::getPodBlFlag, true)
                .ne(BlnoDO::getSiStatus, SiStatusEnum.BookingDraft.getValue());
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);

        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(BlnoConvert.INSTANCE.convertBlnoRespMktDtos(blnoDOList));
    }

    @Override
    public CommonResult<List<BlnoBaseInfoDto>> queryByIds(List<Long> blnoIds) {
        List<BlnoDO> blnoList = blnoMapper.selectList(BlnoDO::getId, blnoIds);
        return CommonResult.success(BeanUtil.copyToList(blnoList, BlnoBaseInfoDto.class));
    }

    @Override
    public CommonResult<Map<Long, List<BlnoScnRespDto>>> queryBlnoScnList(List<Long> blnoIds) {
        //提单收发通信息
        LambdaQueryWrapperX<BlnoScnDO> blnoScnWrapperX = new LambdaQueryWrapperX<>();
        blnoScnWrapperX.inIfPresent(BlnoScnDO::getBlnoId, blnoIds);
        List<BlnoScnDO> blnoScnList = blnoScnMapper.selectList(blnoScnWrapperX);
        if (CollectionUtils.isEmpty(blnoScnList)) {
            return CommonResult.success(Collections.emptyMap());
        }
        List<BlnoScnRespDto> rntList = BeanUtil.copyToList(blnoScnList, BlnoScnRespDto.class);
        Map<Long, List<BlnoScnRespDto>> blnoScnDoMap = rntList.stream().collect(Collectors.groupingBy(BlnoScnRespDto::getBlnoId));
        return CommonResult.success(blnoScnDoMap);
    }

    @Override
    public CommonResult<Map<Long, List<AttachmentUpdateReqDto>>> queryBlnoAttachList(List<Long> blnoIds) {
        List<AttachmentDO> attachmentList = attachmentMapper.selectList(new LambdaQueryWrapperX<AttachmentDO>().in(AttachmentDO::getReferenceId, blnoIds).eq(AttachmentDO::getReferenceType, SoSiEnum.SI.getValue()));
        if (CollectionUtils.isEmpty(attachmentList)) {
            return CommonResult.success(Collections.emptyMap());
        }
        List<AttachmentUpdateReqDto> rntList = BeanUtil.copyToList(attachmentList, AttachmentUpdateReqDto.class);
        Map<Long, List<AttachmentUpdateReqDto>> rntMap = rntList.stream().collect(Collectors.groupingBy(AttachmentUpdateReqDto::getReferenceId));
        return CommonResult.success(rntMap);
    }

    @Override
    public CommonResult<List<String>> queryCsmCodeInfo(String vslCode, String voyCode, String portCode) {
        List<String> result = blnoMapper.getBlnoCsmCodeInfo(vslCode, voyCode, portCode);
        if (CollectionUtils.isEmpty(result)) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(result);
    }

    @Override
    public CommonResult<List<BlnoWebInfoRespDto>> queryBlnoInfoByBlno(BlnoWebReqDto blnoWebReqDto) {
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.inIfPresent(BlnoDO::getBlNo, blnoWebReqDto.getBlnos())
                .eq(BlnoDO::getBkpCode, blnoWebReqDto.getBkpCode())
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .eq(BlnoDO::getPodBlFlag,true)
                .ne(BlnoDO::getSiStatus, SiStatusEnum.BookingDraft.getValue());
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<BlnoWebInfoRespDto> blnoWebInfoRespDtos = BeanUtil.copyToList(blnoDOList, BlnoWebInfoRespDto.class);
        return CommonResult.success(blnoWebInfoRespDtos);
    }

    @Override
    public CommonResult<List<BlnoWebInfoRespDto>> queryBlnoInfoByVslVoy(BlnoWebVslVeyReqDto blnoWebVslVeyReqDto) {
        LambdaQueryWrapperX<BlnoDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        lambdaQueryWrapperX.eq(BlnoDO::getBkgVesselCode, blnoWebVslVeyReqDto.getBkgVesselCode())
                .eq(BlnoDO::getBkgVoyage, blnoWebVslVeyReqDto.getBkgVoyage())
                .eq(BlnoDO::getBkpCode, blnoWebVslVeyReqDto.getBkpCode())
                .eq(BlnoDO::getValid, SiValidEnum.VALID.getValue())
                .ne(BlnoDO::getSiStatus, SiStatusEnum.BookingDraft.getValue());
        List<BlnoDO> blnoDOList = blnoMapper.selectList(lambdaQueryWrapperX);
        if (CollectionUtils.isEmpty(blnoDOList)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<BlnoWebInfoRespDto> blnoWebInfoRespDtos = BeanUtil.copyToList(blnoDOList, BlnoWebInfoRespDto.class);
        return CommonResult.success(blnoWebInfoRespDtos);
    }

    @Override
    public CommonResult<List<BlnoMultipleSoResqVo>> getBlnoMultipleSoDos(Long blnoId) {
        List<BlnoMultipleSoDO> blnoMultipleSoDos = blnoMultipleSoMapper.selectList(new LambdaQueryWrapper<BlnoMultipleSoDO>().eq(BlnoMultipleSoDO::getBlnoId, blnoId));
        return CommonResult.success(BeanUtil.copyToList(blnoMultipleSoDos, BlnoMultipleSoResqVo.class));
    }
}
